/**
 * 小数 x 位
 * @param {*} n
 */
export const parseNumFloat = (num, digit = 2) => {
  let n = Number(num)
  if (Math.floor(n) === n) {
    return n
  } else {
    let p = Math.pow(10, digit)
    return Math.round(n * p) / p
  }
}

// 导出
export const downLoadFile = (res, customFileName = '文件') => {
  if (!res) {
    return
  }
  console.log(res)
  var a = document.createElement('a')

  let url = window.URL.createObjectURL(new Blob([res.data], { type: 'application/octet-stream' }))
  console.log(url)
  a.href = url
  let head = res.headers['content-disposition']
  let fileName = ''
  console.log('导出时候：head是：', head)
  // 因为58上 headFileName是乱码，导致导出的文件名是乱码
  // if (head) {
  //   let headIdx = head.indexOf('=')
  //   let headFileName = head.substr(headIdx + 1, head.length)
  //   // let fileName = '检测机构.xlsx'
  //   console.log('headFileName', headFileName)
  //   fileName = headFileName || customFileName
  // } else {
  //   fileName = customFileName
  // }
  fileName = customFileName

  a.download = decodeURI(fileName)
  // console.log(decodeURI(fileName))
  document.body.appendChild(a)
  a.click()
  document.body.removeChild(a)
  window.URL.revokeObjectURL(url)
}
